/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.socialNet.dao.implementations;

import com.bionic.socialNet.dao.interfaces.IUserConnectionsDAO;
import com.bionic.socialNet.entities.ProfileData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ann
 */
public class OraUserConnections extends QueryExecutor implements IUserConnectionsDAO {

    private final String SELECT_FRIENDS = "select user_id, nickname, fullname, birthday, sex, about, photo from profile p, user_connection uc "
            + " where  uc.user_from = %d and p.user_id = uc.user_to";
    private final String INSERT_CONNECTION = "insert into user_connection "
            + "(connection_id, user_from, user_to, type) values "
            + "(seq_connection.nextval, %d,%d,%d)";//(v_connection_id, v_user_from, v_user_to, v_type);";


    @Override
    public ArrayList<ProfileData> selectFriendsCollection(int userId) {

        ResultSet RS = query.getRSForQuery(String.format(SELECT_FRIENDS, userId));
        ArrayList<ProfileData> friendsList = new ArrayList<ProfileData>();

        try {
            while (RS.next()) {
                ProfileData friendProfile = new ProfileData();
                friendProfile.setAbout(RS.getString("about"));
                friendProfile.setBirthday(RS.getDate("birthday"));
                friendProfile.setFullname(RS.getString("fullname"));
                friendProfile.setNickname(RS.getString("nickname"));
                friendProfile.setPhoto(null);//temporary null then try to make
                friendProfile.setSex(RS.getInt("sex"));
                friendProfile.setUserId(RS.getInt("user_id"));
                friendsList.add(friendProfile);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OraUser.class.getName()).log(Level.SEVERE, null,
                    ex);
            return null;
        }
        return friendsList;

    }

    @Override
    public boolean InsertConnection(int userFrom, int userTo, int type) {
        try {
            query.executeInsertStatement(String.format(INSERT_CONNECTION, userFrom, userTo, type));
            return true;
        } catch (Exception e) {
            return false;
        }



    }
}
